package com.mj.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将xml解析成map
 * @author majie
 *
 */
public class XMLUtil {

	/**
	 * 将微信服务器发送的Request请求中Body的XML解析为Map
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> parseRequestXmlToMap(HttpServletRequest request) throws Exception {
		// 解析结果存储在HashMap中
		Map<String, String> resultMap;
		InputStream inputStream = request.getInputStream();
		resultMap = parseInputStreamToMap(inputStream);
		return resultMap;
	}

	/**
	 * 将输入流中的XML解析为Map
	 *
	 * @param inputStream
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static Map<String, String> parseInputStreamToMap(InputStream inputStream)
			throws DocumentException, IOException {
		// 解析结果存储在HashMap中
		Map<String, String> map = new HashMap<String, String>();
		// 读取输入流
		SAXReader reader = new SAXReader();
		Document document = reader.read(inputStream);
		// 得到xml根元素
		Element root = document.getRootElement();
		// 得到根元素的所有子节点
		List<Element> elementList = root.elements();
		// 遍历所有子节点
		for (Element e : elementList) {
			map.put(e.getName(), e.getText());
		}
		// 释放资源
		inputStream.close();
		return map;
	}

	/**
	 * 将String类型的XML解析为Map
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> parseXmlStringToMap(String str) throws Exception {
		Map<String, String> resultMap;
		InputStream inputStream = new ByteArrayInputStream(str.getBytes("UTF-8"));
		resultMap = parseInputStreamToMap(inputStream);
		return resultMap;
	}
}